package com.neusoft.chenhanhua.d0915;

import java.awt.Graphics;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JFrame;

public class HashSetCanvasPane extends JFrame {
	private String type="l";
  private Shape shape;
  private ShapeArray shapeArray;
	public void setType(String type) {
		this.type=type;
		
	}
	 public HashSetCanvasPane() {
		 //添加点击事件
		 //MouseAdapter属于设配器开发模式
		 System.out.println(type);
		 addMouseListener(ma);
		 addMouseMotionListener(ma);
		 shapeArray=new ShapeArray();
	}
	 private MouseAdapter ma=new MouseAdapter() {
		 //鼠标事件可以让您跟踪鼠标按下，释放，点击，移动，拖动，进入组件，退出时和鼠标滚轮移动时
		 //我的理解主要是跟踪鼠标事件。。。。。
		 //鼠标点击时
		 public void mousePressed(MouseEvent e) {
				System.out.println("mousePressed");
				//获取点击坐标
				if ("l".equals(type)) {
					shape=new Line();
					}else if("s".equals(type)) {
						shape =new Square();
					}
				shape.setX1(e.getX());
				shape.setY1(e.getY());
			}
		 //鼠标松开时调用
			public void mouseReleased(MouseEvent e) {
				System.out.println("mouseReleased");
				shape.setX2(e.getX());
				shape.setY2(e.getY());
				shapeArray.push(shape);
				shape = null;
				repaint();
			}
			//鼠标拖动时调用
			public void mouseDragged(MouseEvent e) {
				shape.setX2(e.getX());
				shape.setY2(e.getY());
				repaint();
			}
	};
	@Override
	public void paint(Graphics g) {
		System.out.println("paint执行了");
		super.paint(g);
		if(shape != null) {
			shape.draw(g);
		}
		shapeArray.draw(g);
	}


}
